package edu.zjut.hjg.saas.biz.service.impl;

import static edu.zjut.hjg.saas.biz.support.AppConstants.IMAGE_RELATIVE_PATH;

import java.io.File;
import java.util.List;

import edu.zjut.hjg.saas.biz.dao.ActivityDao;
import edu.zjut.hjg.saas.biz.dao.AlbumDao;
import edu.zjut.hjg.saas.biz.dao.UserLogDao;
import edu.zjut.hjg.saas.biz.model.Album;
import edu.zjut.hjg.saas.biz.model.UserLog;
import edu.zjut.hjg.saas.biz.service.AlbumService;
import edu.zjut.hjg.saas.biz.support.AlbumType;
import edu.zjut.hjg.saas.biz.support.OperateType;
import edu.zjut.hjg.saas.biz.support.Paginated;

public class AlbumServiceImpl implements AlbumService {

    private AlbumDao    albumDao;
    private ActivityDao activityDao;
    private UserLogDao  userLogDao;
    private String      baseFilePath;

    public void setAlbumDao(AlbumDao albumDao) {
        this.albumDao = albumDao;
    }

    public void setActivityDao(ActivityDao activityDao) {
        this.activityDao = activityDao;
    }

    public void setUserLogDao(UserLogDao userLogDao) {
        this.userLogDao = userLogDao;
    }

    public void setBaseFilePath(String baseFilePath) {
        this.baseFilePath = baseFilePath;
    }

    @Override
    public void create(Album album, String operator) {
        albumDao.create(album);
        UserLog userLog = new UserLog();
        userLog.setKindergarten(album.getKindergarten());
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.INSERT);
        userLog.setDescription("创建了一个相册：" + album.getName());
        userLogDao.create(userLog);
    }

    @Override
    public Paginated<Album> listByAlbumTypeAndAppId(AlbumType albumType, int appId, int pageSize,
                                                    int page) {
        return albumDao.listByAlbumTypeAndAppId(albumType, appId, pageSize, page);
    }

    @Override
    public List<Album> listAllByAlbumTypeAndAppId(AlbumType albumType, int appId) {
        return albumDao.listAllByAlbumTypeAndAppId(albumType, appId);
    }

    @Override
    public void deleteByIdAndAppId(int id, int appId, String operator) {
        Album album = albumDao.findByIdAndAppId(id, appId);
        if (album == null) {
            throw new RuntimeException("no album found for id=" + id + " and appId=" + appId);
        }
        switch (album.getAlbumType()) {
            case ACTIVITY:
                deleteActivitiesByAlbumIdAndAppId(id, appId);
                break;
            default:
                // TODO 其它相册
        }
        albumDao.deleteByIdAndAppId(id, appId);

        UserLog userLog = new UserLog();
        userLog.getKindergarten().setId(appId);
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.DELETE);
        userLog.setDescription("删除了相册：" + album.getName());
        userLogDao.create(userLog);
    }

    private void deleteActivitiesByAlbumIdAndAppId(int id, int appId) {
        activityDao.deleteByAlbumIdAndAppId(id, appId);
        File file = new File(baseFilePath + IMAGE_RELATIVE_PATH + "/" + appId + "/" + id);
        deleteFolder(file);
    }

    private void deleteFolder(File dir) {
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            if (file.isDirectory()) {
                deleteFolder(file);
            } else {
                file.delete();
            }
        }
        dir.delete();
    }

    @Override
    public Album findByIdAndAppId(int id, int appId) {
        return albumDao.findByIdAndAppId(id, appId);
    }
}
